package common

import (
	"encoding/json"
	"fmt"
)
const SUCCESS = 1;
const ERROR   = 0;
type WsRes struct {
	Code      int         `json:"code,omitempty"`
	Data      interface{} `json:"data,omitempty"`
	ErrorCode int         `json:"error_code,omitempty"`
	Action    string      `json:"action,omitempty"`
	Msg       string      `json:"msg,omitempty"`
}



func (r WsRes) ToJson() []byte {
	b,err := json.Marshal(r)
	if err != nil {
		return []byte{}
	}
	return b
}


func SuccJson(msg string, data interface{}) []byte {
	return ResJson(1,0,msg,data)
}

func ErrJson(errorCode int, msg string, data interface{}) []byte {
	return ResJson(0,errorCode,msg,data)
}

func ResJson(code int, errorCode int, msg string, data interface{}) []byte {
	res := map[string]interface{}{}
	res["code"] = code
	res["error_code"] = errorCode
	res["msg"] = msg
	res["data"] = data
	b,err := json.Marshal(res)
	if err != nil {
		fmt.Println("res 转换json出错:"+err.Error())
		return []byte("");
	}
	return b
}

